/*
 * PWM工具函数头文件
 * 功能：定义PWM操作相关的函数和常量
 * 作者：test_code项目
 */

#ifndef PWM_UTILS_H
#define PWM_UTILS_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <dirent.h>

// PWM相关常量定义
#define PWM_SYSFS_BASE_PATH     "/sys/class/pwm"
#define PWM_MAX_PATH_LEN        256
#define PWM_MAX_VALUE_LEN       64

// PWM错误代码
#define PWM_SUCCESS             0
#define PWM_ERROR_INVALID_PARAM -1
#define PWM_ERROR_FILE_ACCESS   -2
#define PWM_ERROR_WRITE_FAILED  -3
#define PWM_ERROR_READ_FAILED   -4
#define PWM_ERROR_NOT_EXPORTED  -5

// PWM状态枚举
typedef enum {
    PWM_STATE_DISABLED = 0,
    PWM_STATE_ENABLED = 1
} pwm_state_t;

// PWM极性枚举
typedef enum {
    PWM_POLARITY_NORMAL = 0,    // 正常极性
    PWM_POLARITY_INVERSED = 1   // 反向极性
} pwm_polarity_t;

// PWM信息结构体
typedef struct {
    int chip;
    int channel;
    unsigned long period;       // 周期 (ns)
    unsigned long duty_cycle;   // 占空比时间 (ns)
    pwm_state_t enabled;
    pwm_polarity_t polarity;
} pwm_info_t;

// 基础PWM操作函数
int pwm_export(int chip, int channel);
int pwm_unexport(int chip, int channel);
int pwm_is_exported(int chip, int channel);

// PWM参数设置函数
int pwm_set_period(int chip, int channel, unsigned long period_ns);
int pwm_set_duty_cycle(int chip, int channel, unsigned long duty_ns);
int pwm_set_polarity(int chip, int channel, pwm_polarity_t polarity);
int pwm_enable(int chip, int channel);
int pwm_disable(int chip, int channel);

// PWM参数读取函数
int pwm_get_period(int chip, int channel, unsigned long *period_ns);
int pwm_get_duty_cycle(int chip, int channel, unsigned long *duty_ns);
int pwm_get_polarity(int chip, int channel, pwm_polarity_t *polarity);
int pwm_get_state(int chip, int channel, pwm_state_t *enabled);

// PWM信息查询函数
int pwm_get_info(int chip, int channel, pwm_info_t *info);
void pwm_print_info(const pwm_info_t *info);

// 系统PWM设备查询函数
int pwm_list_chips(int *chips, int max_chips);
int pwm_get_chip_npwm(int chip);
void pwm_show_system_info(void);

// 高级PWM操作函数
int pwm_set_frequency_duty(int chip, int channel, double freq_hz, double duty_percent);
int pwm_generate_tone(int chip, int channel, double freq_hz, int duration_ms);
int pwm_fade_in_out(int chip, int channel, double freq_hz, int fade_time_ms, int hold_time_ms);

// 工具函数
const char* pwm_error_string(int error_code);
int pwm_validate_parameters(unsigned long period_ns, unsigned long duty_ns);
void pwm_delay_ms(int milliseconds);
void pwm_delay_us(int microseconds);

// PWM计算辅助函数
double pwm_duty_cycle_to_percent(unsigned long period_ns, unsigned long duty_ns);
unsigned long pwm_percent_to_duty_cycle(unsigned long period_ns, double percent);
double pwm_period_to_frequency(unsigned long period_ns);
unsigned long pwm_frequency_to_period(double freq_hz);

// 调试和日志函数
void pwm_debug_print(const char *format, ...);
void pwm_error_print(const char *format, ...);

#endif // PWM_UTILS_H